﻿using Ah.Testudin.Engine.Actors;
using Ah.Testudin.Engine.Actors.Components;
using Ah.Testudin.Engine.Events;
using Ah.Testudin.Engine.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Ah.Testudin.Engine.UI.Elements
{
    public class HealthBarScreenElement : ScreenElementBase
    {
        #region Private Member Variables

        private readonly Texture2D _backgroundTexture;

        private readonly Texture2D _foregroundTexture;

        private readonly Actor _actor;

        private readonly ITestudinRenderer _testudinRenderer;

        private int _widthToDraw;

        private readonly string _playerName;

        private readonly SpriteFont _spriteFont;

        private readonly Vector2 _textPosition;

        #endregion

        #region Constructors

        public HealthBarScreenElement(
            Vector2 position,
            int width,
            int height,
            Texture2D backgroundTexture,
            Texture2D foregroundTexture,
            Actor actor,
            ITestudinRenderer testudinRenderer,
            string playerName,
            SpriteFont spriteFont)
            : base(position, width, height)
        {
            _backgroundTexture = backgroundTexture;

            _foregroundTexture = foregroundTexture;

            _actor = actor;

            _testudinRenderer = testudinRenderer;

            _widthToDraw = _width;

            _playerName = playerName;

            _spriteFont = spriteFont;

            _textPosition = new Vector2(
                position.X - (_playerName.Length * 12),
                position.Y);

            EventManagerSingleton.Instance.RegisterEventHandler(
                EventType.PlayerDied,
                HandlePlayerDiedEvent);
        }

        #endregion

        #region ScreenElementBase

        public override short ZOrder
        {
            get { return 0; }
        }

        public override bool Visible
        {
            get { return true; }
        }

        public override bool ProcessInput()
        {
            return false;
        }

        public override void Update(GameTime gameTime)
        {
            HealthComponent healthComponent = _actor.GetComponent<HealthComponent>();

            float healthRatio = (float)healthComponent.CurrentHealth / healthComponent.MaxHealth;

            _widthToDraw = (int)(healthRatio * _width);
        }

        public override void Render()
        {
            _testudinRenderer.BeginSpriteBatch();

            _testudinRenderer.RenderSprite(
                new Rectangle(
                    (int)_position.X,
                    (int)_position.Y,
                    _width,
                    _height),
                _backgroundTexture);

            _testudinRenderer.RenderSprite(
                new Rectangle(
                    (int)_position.X,
                    (int)_position.Y,
                    _widthToDraw,
                    _height),
                _foregroundTexture);

            _testudinRenderer.RenderString(
                _spriteFont,
                _playerName,
                _textPosition,
                Color.Black);

            _testudinRenderer.EndSpriteBatch();
        }

        #endregion

        #region Private Methods

        private void HandlePlayerDiedEvent(ITestudinEvent testudinEvent)
        {
            PlayerDiedEvent playerDiedEvent = testudinEvent as PlayerDiedEvent;

            if (playerDiedEvent.ActorId == _actor.Id)
            {
                HealthComponent healthComponent = _actor.GetComponent<HealthComponent>();

                healthComponent.ApplyDamage(healthComponent.CurrentHealth);
            }
        }

        #endregion
    }
}
